package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.statistics.SeachApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.TimeStampTool;
import com.fzdy.dataplatform.entity.AdminDeviceStatisticsVo;
import com.fzdy.dataplatform.entity.AdminStatisticsVo;
import com.fzdy.dataplatform.entity.UserDeviceStatisticsVo;
import com.fzdy.dataplatform.entity.UserStatisticsVo;
import com.fzdy.dataplatform.service.vo.AdminDeviceStatisticsVoService;
import com.fzdy.dataplatform.service.vo.AdminStatisticsVoService;
import com.fzdy.dataplatform.service.vo.UserDeviceStatisticsVoService;
import com.fzdy.dataplatform.service.vo.UserStatisticsVoService;
import com.fzdy.dataplatform.webutil.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/sysmanage/statistics")
public class StatisticsController {

    @Autowired
    private WebUtilify webUtilify;
    @Autowired
    private UserStatisticsVoService userStatisticsVoService;
    @Autowired
    private AdminStatisticsVoService adminStatisticsVoService;
    @Autowired
    private UserDeviceStatisticsVoService userDeviceStatisticsVoService;
    @Autowired
    private AdminDeviceStatisticsVoService adminDeviceStatisticsVoService;

    @RequestMapping(value = "/getStatistics")
    public String getStatistics(HttpServletRequest request) {
        AjaxCallback ajaxCallback=new AjaxCallback();
        if (webUtilify.isAdmin(request)){
            AdminStatisticsVo adminStatisticsVo = adminStatisticsVoService.get(1);
            ajaxCallback.setData(adminStatisticsVo);
        }else {
            UserStatisticsVo userStatisticsVo=userStatisticsVoService.get(webUtilify.LoginUserinfo(request).getId());
            ajaxCallback.setData(userStatisticsVo);
        }
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/getIncreaseDeviceNum")
    public String getIncreaseDeviceNum(@RequestBody SeachApi seachApi, HttpServletRequest request) {
        AjaxCallback ajaxCallback=new AjaxCallback();
        List<String> date=new ArrayList<>();
        List<Integer> value=new ArrayList<>();
        JSONObject jsonObject=new JSONObject();
        if (seachApi.getEndDate()==null||seachApi.getStartDate()==null){
            Date endDate=new Date();
            seachApi.setEndDate(TimeStampTool.DateToString("yyyy-MM-dd",endDate));
            String month = TimeStampTool.DateToString("yyyy-MM", endDate);
            seachApi.setStartDate(month+"-01");
        }
        if (webUtilify.isAdmin(request)){
            List<AdminDeviceStatisticsVo> list = adminDeviceStatisticsVoService.getList(seachApi);
            for (AdminDeviceStatisticsVo vo :list) {
                date.add(vo.getCreatedate());
                value.add(vo.getDevicenum());
            }
        }else {
            seachApi.setUserid(webUtilify.LoginUserinfo(request).getId());
            List<UserDeviceStatisticsVo> list = userDeviceStatisticsVoService.getList(seachApi);

            for (UserDeviceStatisticsVo vo :list) {
                date.add(vo.getCreatedate());
                value.add(vo.getDevicenum());
            }
        }
        jsonObject.put("date",date);
        jsonObject.put("value",value);
        ajaxCallback.setData(jsonObject);
        return JSON.toJSONString(ajaxCallback);
    }

}
